package com.sakila.persistence.service.impl;

import java.util.Collection;
import java.util.List;


import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sakila.persistence.entities.Actor;
import com.sakila.persistence.entities.Category;
import com.sakila.persistence.entities.Film;
import com.sakila.persistence.entities.Language;
import com.sakila.persistence.service.InventoryService;
@Service("inventoryService")
@Repository
@Transactional
public class InventoryServiceImpl implements InventoryService {
	
	@PersistenceContext
	private EntityManager entityMgr;

	@Override
	public void setEntityManager(EntityManager entityMgr) {
		this.entityMgr = entityMgr;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Language> findAllLanguages() {
		String query = "select l from Language l";
		return entityMgr.createQuery(query).getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Category> findAllCategory() {
		String query = "select c from Category c";
		return entityMgr.createQuery(query).getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Actor> findAllActor() {
		String query = "select a from Actor a";
		return entityMgr.createQuery(query).getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Film> findAllFilm() {
		String query = "select f from Film f";
		return entityMgr.createQuery(query).getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Film> browseFilmByCategory(Category category) {
		String query = "select fc.film from FilmCategory fc inner join fc.category as category where category.categoryId="
				+ category.getCategoryId();
		List<Film> films = entityMgr.createQuery(query).getResultList();
		return films;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Film> browseFilmByLanguage(Language language) {
		String query = "select f from Film f where f.language.languageId = "
				+ language.getLanguageId();
		List<Film> films = entityMgr.createQuery(query).getResultList();
		return films;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Film> browseFilmByActor(Actor actor) {
		String query = "select fc.film from FilmActor fc inner join fc.actor as actor where actor.actorId="
				+ actor.getActorId();
		List<Film> films = entityMgr.createQuery(query).getResultList();
		return films;
	}
	
	@Override
	public Film findFilmById(int id){
		return entityMgr.find(Film.class, id);
	}
	
	@Override
	public Actor findActorById(int id){
		return entityMgr.find(Actor.class, id);
	}
	
	@Override
	public Category findCategoryById(int id){
		return entityMgr.find(Category.class, id);
	}
	
	@Override
	public boolean removeActorsFromFilm(Film film, Collection<Actor> actors, boolean all){
		return false;
	}

}
